#!/bin/sh
JAVA_OPTS='-Xms512m -Xmx2048m -XX:NewSize=128m -XX:MaxNewSize=212m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=212m -XX:NewRatio=2 -XX:MaxTenuringThreshold=8 -XX:+DisableExplicitGC'
RESOURCE_NAME=/home/guanrun-static/jeecg-system-start-3.4.4.jar
LOG_NAME=/home/guanrun-static/restart.log
MAX_TIMEOUT=20

tpid=`ps -ef|grep $RESOURCE_NAME|grep -v grep|grep -v kill|awk '{print $2}'`

if [ ${tpid} ]; then
        echo 'Stop Process...'
        kill -15 $tpid
fi

for((i=0;i<$MAX_TIMEOUT;i++))
do
        sleep 1
        tpid=`ps -ef|grep $RESOURCE_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
        if [ ${tpid} ]; then
                echo 'App Stoping...'
        else
                break
        fi
done

if [ ${tpid} ]; then
        echo 'Kill Process!'
        kill -9 $tpid
else
        echo 'Stop Success!'
fi

tpid=`ps -ef|grep $RESOURCE_NAME|grep -v grep|grep -v kill|awk '{print $2}'`

if [ ${tpid} ]; then
    echo 'App is running.'
else
    echo 'App is NOT running.'
fi

rm -f tpid

echo 'App is Starting...'
nohup java $JAVA_OPTS -jar $RESOURCE_NAME >$LOG_NAME 2>&1 &
echo $! > tpid
echo Start Success!
